package com.example.ysdt.controller;

import com.example.ysdt.Vo.UserVo;
import com.example.ysdt.entiey.User;
import com.example.ysdt.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Configuration
@RestController
@RequestMapping("/Login")  // 指定映射地址
@Api(value = "用户接口", tags = {"用户接口"})
public class LoginController {

    @Autowired  //注入接口
    LoginService loginService;

    @PostMapping("login")
    @ApiOperation("登录")
    public Map<String,Object> login(@RequestBody UserVo user ){  // 实体类接收前端传参
        // 因为 user传过来为json字符串，所以用@RequestBody 进行实体转换
        Map result=new HashMap();
        // 业务代码在userService里完成
        User data = loginService.Login(user);
        if(data != null){
            result.put("code",0);
            result.put("message","登录成功");

            if (data.getIs_system()!=null){
                result.put("is_system",data.getIs_system());
            }else{
                result.put("is_system",0);
            }
            result.put("id", data.getId());
            return result;
        }
        result.put("code",-1);
        result.put("message","用户名或密码错误");
        return result;
    }


    @PostMapping("register")
    @ApiOperation("注册")
    public Map<String,Object> count(@RequestBody User user){
        Map Result = new HashMap();
        try {
            loginService.register(user);
            Result.put("code",0);
            Result.put("message","注册成功");
        }catch (Exception e){
            Result.put("code",1);
            Result.put("message","注册失败"+e.getMessage());
        }
        return Result;
    }

}
